/**
 * Created by zhangyilin on 2018/11/21.
 */
import java.sql.Connection;
import java.sql.DriverManager;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Task {
    private int TaskID;//任务编号，数据库自增
    private String TaskTheme;//任务主题
    private String TaskIssuTime;//任务发布日期
    private String TaskDeadline;//任务截止日期
    private String TaskGroup;//任务所属组,使用InviteID关联对应
    private String TaskContent;//任务内容

    /*与数据库建立连接*/
    public Connection GetConnection() {//与数据库建立连接
        String url = "jdbc:mysql://localhost:3306/listsystem";//与本机的3306端口建立连接，进入数据库listsystem
        String driverName = "com.mysql.jdbc.Driver";//驱动注册
        String userName = "root";
        String password = "";
        Connection conn = null;//用于建立与数据库的连接
        try{
            Class.forName(driverName);// jdbc4.0 后无需使用这句进行驱动注册操作
            conn = DriverManager.getConnection(url, userName, password);
        }catch(Exception e){
            System.out.println(e);
        }
        return conn;
    }

    /*创建新任务*/
    public Task(String taskTheme,String taskDeadline,String taskGroup,String taskContent) {
        setTaskTheme(taskTheme);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        setTaskIssuTime(df.format(new Date()));//获得当前日期
        setTaskDeadline(taskDeadline);
        setTaskGroup(taskGroup);
        setTaskContent(taskContent);
        Connection conn = GetConnection();
        PreparedStatement taskPS = null;
        String taskSql = "insert into task_teacher values(null,?,?,?,?)";//新建任务
        if(conn != null)
            try {
                taskPS = conn.prepareStatement(taskSql);
                if(taskPS != null) {
                    taskPS.setString(1, getTaskTheme());
                    taskPS.setString(2, getTaskIssuTime());
                    taskPS.setString(3, getTaskDeadline());
                    taskPS.setString(4, getTaskGroup());
                    taskPS.execute();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(taskPS != null)
                        taskPS.close();
                    if(conn != null)
                        conn.close();
                    taskPS = null;
                    conn = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        conn = GetConnection();
        PreparedStatement getTaskIDPS = null;
        ResultSet getTaskIDRS = null;//查询获取任务ID号
        String getTaskIDSql = "select TaskID from task_teacher where TaskTheme=? and TaskIssuTime=? and TaskDeadline=? and TaskGroup=?";
        if(conn != null)
            try {
                getTaskIDPS = conn.prepareStatement(getTaskIDSql);
                if(getTaskIDPS != null) {
                    getTaskIDPS.setString(1, getTaskTheme());
                    getTaskIDPS.setString(2, getTaskIssuTime());
                    getTaskIDPS.setString(3, getTaskDeadline());
                    getTaskIDPS.setString(4, getTaskGroup());
                    getTaskIDRS = getTaskIDPS.executeQuery();
                    if(getTaskIDRS != null && getTaskIDRS.next()) {
                        TaskID = Integer.parseInt(getTaskIDRS.getString("TaskID"));
                        setTaskID(TaskID);
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(getTaskIDRS != null)
                        getTaskIDRS.close();
                    if(getTaskIDPS != null)
                        getTaskIDPS.close();
                    if(conn != null)
                        conn.close();
                    getTaskIDRS = null;
                    getTaskIDPS = null;
                    conn = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        conn = GetConnection();
        PreparedStatement teacherContentPS = null;//记录任务详细内容
        String teacherContentSql = "insert into taskcontent_teacher(TaskID,TaskContent) values(?,?)";
        if(conn != null)
            try {
                teacherContentPS = conn.prepareStatement(teacherContentSql);
                if(teacherContentPS != null) {
                    teacherContentPS.setInt(1, getTaskID());
                    teacherContentPS.setString(2, getTaskContent());
                    teacherContentPS.execute();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(teacherContentPS != null)
                        teacherContentPS.close();
                    if(conn != null)
                        conn.close();
                    teacherContentPS = null;
                    conn = null;
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        conn=GetConnection();//查询该班级都有哪些学生
        String stuIDSql = "select StudentID from CStudent where InviteID=?";
        PreparedStatement stuIDPS = null;
        ResultSet stuIDRS = null;
        String[] studentIDs = null;
        try {
            if(conn != null)
                stuIDPS = conn.prepareStatement(stuIDSql);
            if(stuIDPS != null)
                stuIDPS.setString(1, getTaskGroup());
            stuIDRS = stuIDPS.executeQuery();
            if(stuIDRS !=null && stuIDRS.next())
                studentIDs = stuIDRS.getString("StudentID").split(",");
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                if(stuIDRS != null)
                    stuIDRS.close();
                if(stuIDPS != null)
                    stuIDPS.close();
                if(conn != null)
                    conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                stuIDRS = null;
                stuIDPS = null;
                conn = null;
            }
        }
        conn = GetConnection();//往学生任务表中插入新任务内容
        PreparedStatement stuContentPS = null;//记录任务详细内容
        String stuContentSql = null;
        if(studentIDs != null) {
            for (int i = 0; i < studentIDs.length; i++) {
                stuContentSql = "insert into taskcontent_stu(StudentID,TaskID,TaskContent) values(?,?,?)";
                if(conn != null)
                    try {
                        stuContentPS = conn.prepareStatement(stuContentSql);
                        if(stuContentPS != null) {
                            stuContentPS.setString(1, studentIDs[i]);
                            stuContentPS.setInt(2, getTaskID());
                            stuContentPS.setString(3, getTaskContent());
                            stuContentPS.execute();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
            }
            try {
                if(stuContentPS != null)
                    stuContentPS.close();
                if(conn != null)
                    conn.close();
                stuContentPS = null;
                conn = null;
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public String getTaskContent() {
        return TaskContent;
    }
    public void setTaskContent(String taskContent) {
        TaskContent = taskContent;
    }
    public int getTaskID() {
        return TaskID;
    }
    public void setTaskID(int taskID) {
        TaskID = taskID;
    }
    public String getTaskTheme() {
        return TaskTheme;
    }
    public void setTaskTheme(String taskTheme) {
        TaskTheme = taskTheme;
    }
    public String getTaskIssuTime() {
        return TaskIssuTime;
    }
    public void setTaskIssuTime(String taskIssuTime) {
        TaskIssuTime = taskIssuTime;
    }
    public String getTaskDeadline() {
        return TaskDeadline;
    }
    public void setTaskDeadline(String taskDeadline) {
        TaskDeadline = taskDeadline;
    }
    public String getTaskGroup() {
        return TaskGroup;
    }
    public void setTaskGroup(String taskGroup) {
        TaskGroup = taskGroup;
    }

    @Override
    public String toString() {
        return "<html>"
                + "<head>"
                + "<title>创建成功</title>"
                + "</head>"
                + "<body>"
                + "<center>"
                + "Task [TaskID=" + TaskID + ", TaskTheme=" + TaskTheme + ", TaskIssuTime=" + TaskIssuTime
                + ", TaskDeadline=" + TaskDeadline + ", TaskGroup=" + TaskGroup
                + ", TaskContent=" + TaskContent + "]"+"<br>"
                + "<input type=\"button\" value=\"返回\" onclick=\"window.location.href='createTask10.jsp'\"/>"
                + "</center>"
                + "</body>"
                + "</html>";
    }
}